import {createRouter, createWebHashHistory} from 'vue-router'

/* Layout */
import Layout from '@/layout/index'

/* Router Modules */
// import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles), 调整为页面动态配置权限，不在需要此字段。
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
    {
        path: '/redirect',
        component: Layout,
        hidden: true,
        children: [
            {
                path: '/redirect/:path(.*)',
                component: () => import('@/views/redirect/index.vue')
            }
        ]
    },
    {
        path: '/login',
        component: () => import('@/views/login/index.vue'),
        hidden: true
    },
    {
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        children: [
            {
                path: 'dashboard',
                component: () => import('@/views/dashboard/index.vue'),
                name: 'Dashboard',
                meta: {title: 'Dashboard', icon: 'dashboard', affix: true}
            }
        ]
    },
    // 此写法解决动态路由页面刷新的 warning 警告
    {
        path: '/:pathMatch(.*)*',
        component: () => import('@/views/error-page/404.vue'),
        hidden: true
    }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */

/**
 * name：外层路由name必填，退出登录的时候要根据 name 清除动态添加的路由信息，内层的 name 用于页面缓存使用
 */
export const asyncRoutes = [
    {
        path: '/user',
        component: Layout,
        name: 'User',
        children: [
            {
                path: 'index',
                component: () => import('@/views/user/index.vue'),
                name: 'UserIndex',
                meta: {
                    title: '用户管理',
                    icon: 'user'
                }
            }
        ],
        mata: {roles: ['admin']}
    },
    {
        path: '/teacher',
        component: Layout,
        name: 'Teacher',
        children: [
            {
                path: 'index',
                component: () => import('@/views/teacher/index.vue'),
                name: 'TeacherIndex',
                meta: {
                    title: '讲师管理',
                    icon: 'user',
                    roles: ['admin']

                }
            }
        ],
        mata: {roles: ['admin']}
    },
    {
        path: '/learnData',
        component: Layout,
        name: 'LearnData',
        mata: {roles: ['admin']},
        children: [
            {
                path: 'index',
                component: () => import('@/views/learnData/index.vue'),
                name: 'LearnDataIndex',
                meta: {
                    title: '学习资料管理',
                    icon: 'table',
                    roles: ['admin']
                }
            }
        ],
    },
    {
        path: '/course',
        component: Layout,
        name: 'Course',
        mata: {roles: ['admin']},
        children: [
            {
                path: 'index',
                component: () => import('@/views/course/index.vue'),
                name: 'CourseIndex',
                meta: {
                    title: '课程管理',
                    icon: 'user'
                }
            }
        ],
    },
    {
        path: '/classRoom',
        component: Layout,
        name: 'classRoom',
        mata: {roles: ['admin']},
        children: [
            {
                path: 'index',
                component: () => import('@/views/classRoom/index.vue'),
                name: 'ClassRoomIndex',
                meta: {
                    title: '教室管理',
                    icon: 'lock'
                }
            }
        ],
    },
    {
        path: '/grade',
        component: Layout,
        name: 'Grade',
        mata: {roles: ['teacher:primary', 'teacher:gold', 'teacher:supper']},
        children: [
            {
                path: 'index',
                component: () => import('@/views/grade/index.vue'),
                name: 'GradeIndex',
                meta: {
                    title: '班级管理',
                    icon: 'user'
                }
            }
        ],
    },
    {
        path: "/courseInfo",
        component: Layout,
        name: "CourseInfo",
        mata: {roles: ["user"]},
        children: [
            {
                path: 'index',
                component: () => import('@/views/courseInfo/index.vue'),
                name: 'InfoIndex',
                meta: {
                    title: "课程信息",
                    icon: "user"
                },
            }
        ]
    },
    // 个人中心
    {
        path: '/myData',
        component: Layout,
        name: 'MyData',
        mata: {roles: ['组员写的什么狗屎']},
        children: [
            {
                path: 'index',
                component: () => import('@/views/myData/index.vue'),
                name: 'MyDataIndex',
                meta: {
                    title: "个人中心",
                    icon: "user"
                }
            }
        ],
    },
//     上课中心
    {
        path: '/classCenter',
        component: Layout,
        name: "ClassCenter",
        mata: {roles: ['teacher:primary', 'teacher:gold', 'teacher:supper']},
        children: [
            {
                path: "/prepare/index",
                component: () => import("@/views/learnData/index.vue"),
                name: "prepare",
                meta: {
                    title: "备课",
                    icon: "user"
                }
            },
            {
                path: "/class/index",
                component: () => import("@/views/classCenter/class/index.vue"),
                name: "class",
                meta: {
                    title: "上课",
                    icon: "user"
                }
            }
        ],
        meta: {
            title: "上课中心",
            icon: "user"
        }

    }

]

const router = createRouter({
    history: createWebHashHistory(),
    routes: constantRoutes
})

// 重置路由为静态路由
export const resetRouter = () => {
    router.getRoutes().forEach((route) => {
        const {name} = route
        if (name && asyncRoutes.find((item) => item.name === name)) {
            router.removeRoute(name)
        }
    })
}

export default router
